package com.lc.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liuc
 * @date 2021/8/30
 * @description
 **/
@Configuration
public class ESConfig {

    @Value("${spring.elasticsearch.rest.uris}")
    private  String  hosts;

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        String[] arr = hosts.split(",");
        // 创建HttpHost数组  封装es的主机和端口
        HttpHost[]  httpHosts  = new  HttpHost[arr.length];
        int i = 0;
        for (String s : arr) {
            String[] sa = s.split(":");
            httpHosts[i++] = new HttpHost(sa[0],Integer.parseInt(sa[1]),"http");
        }
        return   new RestHighLevelClient(RestClient.builder(httpHosts));
    }
}
